
clear
set more off

********************************************************************************
*                      SEPARATIONS: MAIN ESTIMATES                             *
********************************************************************************


***   1.   NATIVE HUSBAND - FOREIGN WIFE   ***

use "$data\MS_final_dataset.dta" , clear

destring ym, replace
gen year= ym 
cap drop HOM 
gen HOM = hbc==wbc
lab var HOM "Homogamous"
gen HET = HOM!=1
lab var HET "Heterogamous"
gen HET_IT =  (hbc==0 & wbc>0) | ( hbc>0 & wbc==0) 
lab var HET_IT "Heterogamous-Italian"
gen HETW = HET==1 & wbc!=0
gen HETH = HET==1 & hbc!=0


*  Marriages with NATIVE HUSBAND 
keep if hbc==0

* Treatment
gen nat= wbc
merge m:1 nat using "$data\CodiciStatiEsteri.dta" , keepusing(EUII)
tab _merge
drop _merge
replace EUII=4 if nat==224 
replace EUII=4 if nat==258  
replace EUII=4 if nat==259  
replace EUII=4 if nat==260  
replace EUII=4 if nat==261  
replace EUII=6 if nat==317  
recode EUII (.=0)
lab define eui3 0 "Italian" 1 "EU15"  2 "EU2004" 3 "EU2007" 4 "EU_Other" 5 "Africa" 6 "Asia" 7 "America_South" 8 "OECD" 
label values EUII eui3

tab EUII, gen(euii)
rename euii1 Italian
rename euii2 eu15
rename euii3 eu2004
rename euii4 eu2007
rename euii5 euother
rename euii6 africa
rename euii7 asia
rename euii8 americas
rename euii9 oecd

drop if msdate==.
gen id = _n 
gen sepyear = yofd(date(sepdate, "DMY"))

stset msdate, origin(time mmdate) failure(separated) id(id)
recode _d(.=1)
recode _t(.=0)

* Assumption: immediate impact (same results when using 2 years) ***

stsplit post2004m5, after(531) at(1)
stsplit post2005m5, after(543) at(1)
stsplit post2007m1, after(563) at(1)
stsplit post2008m1, after(575) at(1)

gen eu2004y1=eu2004==1&(post2004m5==1&post2005m5==0&post2007m1==0&post2008m1==0)
gen eu2004post1=eu2004==1&(post2005m5==1)
gen eu2007y1=eu2007==1&(post2007m1==1&post2008m1==0)
gen eu2007post1=eu2007==1&(post2008m1==1)

egen newEU=rmax(eu2004 eu2007)
gen newEUy1=((eu2004==1&(post2004m5==1&post2005m5==0&post2007m1==0&post2008m1==0))|(eu2007==1&(post2007m1==1&post2008m1==0)))
gen newEUpost1=((eu2004==1&post2005m5==0)|(eu2007==1&post2008m1==1))
gen newEUbefore=((eu2004==1&year<=2002)|(eu2007==1&year<=2005)) 
gen newEUafter=((eu2004==1&year>=532)|(eu2007==1&year>=2007)) 

********************************************************************************
*  Table 6: Columns 1-4 
*  Hazard rate of separations before and after the EU enlargements, semi-parametric Cox model

stcox newEU post2004 post2005 post2007 post2008 newEUy1 newEUpost1, robust strata(HET provres)
outreg2 using "$output\separation_newEU.xls" , keep(newEU newEUy1 newEUpost1) bdec(3) replace 
stcox newEU newEUbefore newEUafter, robust strata(HET year provres)
outreg2 using "$output\separation_newEU.xls" , keep(newEU newEUbefore newEUafter) bdec(3) append 
stcox newEU post2004 post2005 post2007 post2008 newEUy1 newEUpost1 newEUbefore newEUafter, robust strata(HET year provres)
outreg2 using "$output\separation_newEU.xls" , keep(newEU newEUy1 newEUpost1 newEUbefore newEUafter) bdec(3) append 
stcox newEU post2004 post2005 post2007 post2008 newEUy1 newEUpost1 newEUbefore newEUafter Qw, robust strata(HET year provres)
outreg2 using "$output\separation_newEU.xls" , keep(newEU newEUy1 newEUpost1 newEUbefore newEUafter Qw) bdec(3) append 



***   2.   NATIVE WIFE - FOREIGN HUSBAND    ***

clear
set more off
use "$data\MS_final_dataset.dta" , clear

destring ym, replace
gen year= ym 
cap drop HOM 
gen HOM = hbc==wbc
lab var HOM "Homogamous"
gen HET = HOM!=1
lab var HET "Heterogamous"
gen HET_IT =  (hbc==0 & wbc>0) | ( hbc>0 & wbc==0) 
lab var HET_IT "Heterogamous-Italian"
gen HETW = HET==1 & wbc!=0
gen HETH = HET==1 & hbc!=0

* Keep NATIVE WIFE 
 keep if wbc==0

* Treatment
gen nat= hbc
*drop _merge
merge m:1 nat using "$data\CodiciStatiEsteri.dta" , keepusing(EUII)
tab _merge
drop _merge
replace EUII=4 if nat==224  
replace EUII=4 if nat==258  
replace EUII=4 if nat==259  
replace EUII=4 if nat==260  
replace EUII=4 if nat==261  
replace EUII=6 if nat==317  
recode EUII (.=0)
lab define eui3 0 "Italian" 1 "eu15"  2 "eu10" 3 "eu2" 4 "euother" 5 "africa" 6 "asia" 7 "america_south" 8 "oecd" 
label values EUII eui3

tab EUII, gen(euii)
rename euii1 Italian
rename euii2 eu15
rename euii3 eu2004
rename euii4 eu2007
rename euii5 euother
rename euii6 africa
rename euii7 asia
rename euii8 americas
rename euii9 oecd

drop if msdate==.
gen id = _n 
gen sepyear = yofd(date(sepdate, "DMY"))

tab EUII
tab sepyear if eu2004==1 & separated==1
stset msdate, origin(time mmdate) failure(separated) id(id)
recode _d(.=1)
recode _t(.=0)

* Assumption: immediate impact 

stsplit post2004m5, after(531) at(1)
stsplit post2005m5, after(543) at(1)
stsplit post2007m1, after(563) at(1)
stsplit post2008m1, after(575) at(1)

gen eu2004y1=eu2004==1&(post2004m5==1&post2005m5==0&post2007m1==0&post2008m1==0)
gen eu2004post1=eu2004==1&(post2005m5==1)
gen eu2007y1=eu2007==1&(post2007m1==1&post2008m1==0)
gen eu2007post1=eu2007==1&(post2008m1==1)

egen newEU=rmax(eu2004 eu2007)
gen newEUy1=((eu2004==1&(post2004m5==1&post2005m5==0&post2007m1==0&post2008m1==0))|(eu2007==1&(post2007m1==1&post2008m1==0)))
gen newEUpost1=((eu2004==1&post2005m5==0)|(eu2007==1&post2008m1==1))
gen newEUbefore=((eu2004==1&year<=2002)|(eu2007==1&year<=2005)) 
gen newEUafter=((eu2004==1&year>=532)|(eu2007==1&year>=2007)) 


********************************************************************************
*  Table 6: Columns 5-8 
*  Hazard rate of separations before and after the EU enlargements, semi-parametric Cox model

stcox newEU post2004 post2005 post2007 post2008 newEUy1 newEUpost1, robust strata(HET provres)
outreg2 using "$output\separation_newEU.xls" , keep(newEU newEUy1 newEUpost1) bdec(3) append 
stcox newEU newEUbefore newEUafter, robust strata(HET year provres)
outreg2 using "$output\separation_newEU.xls" , keep(newEU newEUbefore newEUafter) bdec(3) append 
stcox newEU post2004 post2005 post2007 post2008 newEUy1 newEUpost1 newEUbefore newEUafter, robust strata(HET year provres)
outreg2 using "$output\separation_newEU.xls" , keep(newEU newEUy1 newEUpost1 newEUbefore newEUafter) bdec(3) append 
stcox newEU post2004 post2005 post2007 post2008 newEUy1 newEUpost1 newEUbefore newEUafter Qh, robust strata(HET year provres)
outreg2 using "$output\separation_newEU.xls" , keep(newEU newEUy1 newEUpost1 newEUbefore newEUafter Qh) bdec(3) append 



********************************************************************************
*                  SEPARATIONS: KAPLAN MEYER ESTIMATES                         *
********************************************************************************

use "$data\MS_final_dataset.dta" , clear
destring ym, replace
gen year= ym 

* Drop marriages of immigrants 
drop if hbc!=0&wbc!=0

* Treatment
gen nat= wbc if hbc==0
merge m:1 nat using "$data\CodiciStatiEsteri.dta" , keepusing(EUII)
tab _merge
drop _merge
rename EUII wEUII
replace wEUII=4 if nat==224  
replace wEUII=4 if nat==258  
replace wEUII=4 if nat==259  
replace wEUII=4 if nat==260  
replace wEUII=4 if nat==261  
replace wEUII=6 if nat==317  
recode wEUII (.=0)

drop nat
gen nat= hbc if wbc==0
merge m:1 nat using "$data\CodiciStatiEsteri.dta" , keepusing(EUII)
tab _merge
drop _merge
rename EUII hEUII
replace hEUII=4 if nat==224 
replace hEUII=4 if nat==258  
replace hEUII=4 if nat==259  
replace hEUII=4 if nat==260  
replace hEUII=4 if nat==261  
replace hEUII=6 if nat==317  
recode hEUII (.=0)

gen EUII=hEUII if hEUII!=0
replace EUII=wEUII if wEUII!=0
mvencode EUII, mv(0)
lab define eui4 0 "Italian" 1 "EU15"  2 "EU2004" 3 "EU2007" 4 "EU_Other" 5 "Africa" 6 "Asia" 7 "America_South" 8 "OECD" 
label values EUII eui4
tab EUII, gen(euii)
rename euii1 italian
rename euii2 eu15
rename euii3 eu2004
rename euii4 eu2007
rename euii5 euother
rename euii6 africa
rename euii7 asia
rename euii8 americas
rename euii9 oecd

gen eui =.
replace eui=0 if euii==0
replace eui=1 if euii==1 
replace eui=2 if euii==2
replace eui=3 if euii==3
replace eui=4 if euii==4
replace eui=5 if euii>=5

egen newEU=rmax(eu2004 eu2007)
drop if msdate==.
gen id = _n 
gen sepyear = yofd(date(sepdate, "DMY"))

* Random sample of Italians 
sample 1 if hbc==0&wbc==0
gen weight=1
replace weight=100 if hbc==0&wbc==0

stset msdate [iw=weight], origin(time mmdate) failure(separated) id(id) 
recode _d(.=1)
recode _t(.=0)

********************************************************************************
*  Figure A2
*  Survival function of marriages

sts list , survival by(eui) at(100) saving("$output\survival_list" , replace)
sts graph, survival by(eui) xline(24) xlabel(0(24)180) yscale(range(0.8 1)) ylabel(0.8(0.05)1) saving("$output\kaplanmeyer", replace)
ltable msdate separated , hazard by(eui) saving("$output\hazardatable", replace)
sts graph , haz nob by(eui) xline(24) xlabel(0(24)180) saving("$output\hazard", replace)


* Keep separations
preserve

tab separated
keep if separated==1

egen All_other_countries=rsum(africa asia oecd americas)
drop if year<2002
sort year
keep italian eu15 eu2004 eu2007 euother All_other_countries year
egen tot=rsum(italian eu15 eu2004 eu2007 euother All_other_countries year)

foreach var in italian eu15 eu2004 eu2007 euother All_other_countries {
replace `var'=`var'/tot
gen `var'2002=`var' if year==2002
egen `var'20020=mean(`var'2002)
replace `var'=`var'/`var'20020
drop `var'2002*
}

********************************************************************************
*  Figure 5: Panel b. Separation rates
*  Marriages and separations, homogamous native couples and heterogamous couples formed by natives and immigrants, by area of origin of the foreign spouse

gen cc1=1.75 if year>=2004 & year<=2005
gen cc2=1.75 if year>=2007 & year<=2008
gr twoway (area cc1 year , bcolor(gs15)) ///
          (area cc2 year , bcolor(gs14)) ///
          (scatter italian year, c(l) lc(lavender) mc(lavender)  m(S) msize(small)) ///
          (scatter eu2004 year, c(1) lc(cranberry) mc(cranberry) m(D) msize(small)) ///     
          (scatter eu2007 year, c(1) lc(dknavy) mc(dknavy) m(O) msize(small)) ///
		  (scatter eu15 year, c(1) lc(dkorange) mc(dkorange) lpattern(dash)  m(S) msize(small)) ///
		  (scatter euother year, c(1) lc(forest_green) mc(forest_green)  lpattern(dot) m(T) msize(small)) ///
  		  (scatter All_other_countries year, c(1) lc(black) mc(black)  lpattern(dash) m(X)), ///
		  graphregion(color(white))  ///
legend(label(3 "Italian") label(4 "EU2004") label(5 "EU2007") label(6 "EU15") label(7 "Rest_of_Europe") label(8 "All other countries") order(4 5 7 3 6 8) r(2)  size(*0.9) region(lcolor(none))) ///
xsca(r(2002 2012)) xlab(2002(1)2012 , labs(small)) ///
ysca(r(0.75 1.75)) ylab(0.75(0.25)1.77 , labs(small) grid) ///
ytitle("Separation Rate" " ", size(*0.9) ) xtitle("")
gr save   "$output\graph_separations.gph" , replace
gr export "$output\graph_separations.pdf" , replace

restore



